import _ from 'lodash';
import { ipcMain, Event, BrowserWindow, app } from 'electron';
import IElectronModule from './bg_base';


export const DEVTOOL_TOGGLE = "devtool-toggle";
export const DEVTOOL_TOGGLE_RET = "devtool-toggle-ret";
export const DEVTOOL_IS_OPEN = "devtool-is-open";
export const DEVTOOL_IS_OPEN_RET = "devtool-is-open-ret";

class ElectornHelper implements IElectronModule {

    private win?: BrowserWindow;

    install(win: Electron.BrowserWindow): void {
        this.win = win;
        ipcMain.on(DEVTOOL_TOGGLE, this.devtoolToggle.bind(this));
        ipcMain.on(DEVTOOL_IS_OPEN, this.isOpen.bind(this));

    }

    private devtoolToggle(event: Event) {
        if (event.sender.isDevToolsOpened) {
            event.sender.closeDevTools();
        }
        else {
            event.sender.openDevTools();
        }
        event.sender.send(DEVTOOL_IS_OPEN_RET, {
            status: "success",
            message: "",
            is_open: event.sender.isDevToolsOpened
        });
    }

    private isOpen(event: Event) {
        event.sender.send(DEVTOOL_IS_OPEN_RET, {
            status: "success",
            message: "",
            is_open: event.sender.isDevToolsOpened
        });
    }

    /**
     * 获取网站是否在运行中。
     * @param event 
     * @param param1 
     */
    private isHttpOn(event: Event, { post = 8081 }: { post?: number } = {}) {
    }
}

export default new ElectornHelper();